package com.kingdee.eas.cp.bc.cost;

import java.math.BigDecimal;

import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.eventbus.event.action.KDActionEvent;
import com.kingdee.bos.webframework.exception.WafException;
import com.kingdee.bos.webframework.dynamic.application.WebSymbolConstant;
import com.kingdee.bos.webframework.dynamic.handler.AbstractWebEventHandler;
import com.kingdee.bos.eventbus.event.action.IActionEventHandler;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.basedata.assistant.SettlementTypeInfo;
import com.kingdee.eas.cp.bc.BizAccountBill;
import com.kingdee.eas.cp.bc.BizAccountBillAccountEntryCollection;
import com.kingdee.eas.cp.bc.BizAccountBillAccountEntryInfo;
import com.kingdee.eas.cp.bc.BizAccountBillEntryCollection;
import com.kingdee.eas.cp.bc.BizAccountBillEntryInfo;
import com.kingdee.eas.cp.bc.BizAccountBillInfo;
import com.kingdee.eas.cp.bc.DailyLoanBillCollection;
import com.kingdee.eas.cp.bc.DailyLoanBillFactory;
import com.kingdee.eas.cp.bc.DailyLoanBillInfo;
import com.kingdee.eas.cp.bc.StateEnum;
import com.kingdee.eas.custom.basedata.MeetingCostInfo;
import com.kingdee.eas.custom.cost.BorrowingType;

public class SubmitActionEvent extends AbstractWebEventHandler implements IActionEventHandler {
	public void onAction(KDActionEvent event) throws WafException,BOSException {
		ModelMap modelMap = event.getReqeustContext().get(
				ModelMap.class);
		BizAccountBillInfo model = (BizAccountBillInfo) modelMap.get(WebSymbolConstant.MODEL_ATTRIBUTE_KEY);
		
		//费用明细校验
		BizAccountBillEntryCollection entryCollection = model.getEntries();
		for(int i=0;i<entryCollection.size();i++){
			BizAccountBillEntryInfo entey = entryCollection.get(i);
			if(entey.getProject().getName().equals("会议费")){
				String msg="费用明细的第"+(i+1)+"行";
				if(null==entey.get("meetingCost")){
					throw new WafException(msg+"会议费标准不能为空!");
				}
				
				if(null==entey.get("dayNumber")){
					throw new WafException(msg+"会议天数不能为空!");
				}
				
				if(null==entey.get("personNumber")){
					throw new WafException(msg+"参会人数不能为空!");
				}
				
				if(null==entey.get("quarterage")){
					throw new WafException(msg+"住宿费不能为空!");
				}
				
				if(null==entey.get("boardWages")){
					throw new WafException(msg+"伙食费不能为空!");
				}
				
				if(null==entey.get("otherExpenses")){
					throw new WafException(msg+"其它费用不能为空!");
				}
				
				MeetingCostInfo meetingCostInfo=(MeetingCostInfo)entey.get("meetingCost");
				BigDecimal standard=meetingCostInfo.getCostnorm().multiply(entey.getBigDecimal("dayNumber")).multiply(entey.getBigDecimal("personNumber"));
				BigDecimal cost=entey.getBigDecimal("quarterage").add(entey.getBigDecimal("boardWages")).add(entey.getBigDecimal("otherExpenses"));
				if(standard.compareTo(cost)<0){
					throw new WafException(msg+"报销费用已超过会议费标准，请重新填写!");
				}
			}
		}
		
		//收款信息校验
		BizAccountBillAccountEntryCollection accountEntryCollection = model.getCollectionEntries();
		for(int i=0;i<accountEntryCollection.size();i++){
			BizAccountBillAccountEntryInfo entey=accountEntryCollection.get(i);
			SettlementTypeInfo settlementTypeInfo = entey.getPayMode();
			if(null!=settlementTypeInfo){
				String msg="收款信息的第"+(i+1)+"行";
//				if(settlementTypeInfo.getName().equals("转账到公司")){
//					if(null==entey.get("identityCard")){
//						throw new WafException(msg+"身份证号码不能为空!");
//					}
//				}
				
				if(settlementTypeInfo.getName().equals("转账到个人")){
					if(null==entey.getPayerName()){
						throw new WafException(msg+"收款人不能为空!");
					}
					
					if(null==entey.getPayerBank()){
						throw new WafException(msg+"开户银行不能为空!");
					}
					
//					if(null==entey.getAccountOpenArea()){
//						throw new WafException(msg+"开户地不能为空!");
//					}
					
					if(null==entey.getPayerAccount()){
						throw new WafException(msg+"银行账号不能为空!");
					}
					
					if(null==entey.get("identityCard")){
						throw new WafException(msg+"身份证号码不能为空!");
					}
				}
			}
				
		}
		
	}
}